<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { contentApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import welcomeForm from './welcomeForm.vue';
  import Pagination from "@/components/Pagination/index.vue";
  const welcomeFormRef = ref();
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page: 1,
    limit: 20,
    content:'',
    data_type:0 as number, // 类型 1=用户 2=主播
  })
  const options = [
    {type:1,lable:'用户'},
    {type:2,lable:'主播'}
  ]
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const handelOpenForm = (row:any = {})=>{
    welcomeFormRef.value.openDialog({
      ...row,
      options
    })
  }
  const handleDelete = (row:any = {})=>{
    ElMessageBox.confirm("确定删除快捷语吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await contentApi.delShortcutWord({
        id:row.id
      });
      loading.close();
      ToastSuccess("删除成功!");
      getList();
    })
    .catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await contentApi.listShortcutWord(listQuery)
    loading.close()
    const { data=[]} = res?.data || {}
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">快捷语内容：</div>
          <el-input v-model="listQuery.content" placeholder="请输入快捷语内容"/>
        </div>
        <div class="labelItem">
          <div class="label">快捷语类型：</div>
          <el-select v-model="listQuery.data_type" placeholder="请选择快捷语类型">
            <el-option label="全部" :value="0"/>
            <el-option
              v-for="item in options"
              :label="item.lable" :value="item.type"/>
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="handelOpenForm()">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="80" prop="id" label="ID" />
      <el-table-column width="80" prop="sort" label="排序" />
      <el-table-column min-width="200" prop="content" label="快捷语内容" />
      <el-table-column width="120" prop="data_type" label="快捷语类型">
        <template #default="scoped">
          {{ options.find(res=>res.type == scoped.row.data_type)?.lable || scoped.row.data_type }}
        </template>
      </el-table-column>
      <el-table-column width="180" prop="admin_name" label="创建人" />
      <el-table-column fixed="right" width="180" label="操作">
        <template #default="scoped">
          <el-button type="primary" @click="handelOpenForm(scoped.row)">编辑</el-button>
          <el-button v-if="scoped.row.status!='deleted'" type="danger" @click="handleDelete(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <welcomeForm ref="welcomeFormRef" @update-list="getList" />
  </div>
</template>